import json
import requests as rq
import module


class ClientAPIBind:

    def __init__(self):
        self.token = ""
        self.uuid = ""
        self.rootPath = "http://localhost/"
        self.header = {"Content-Type": "application/json"}

    def __del__(self):
        if self.uuid != "":
            self.Logout()

    def GetUUID(self) -> str:
        return self.uuid

    def Connect(self, host: str) -> bool:
        self.rootPath = "http://"+host+'/'
        res = rq.get(self.rootPath+"Hello")
        if res.status_code == 200:
            return True
        else:
            return False

    def Login(self, token: str) -> bool:
        self.token = token
        res = rq.post(self.rootPath + "Login",
                      params={"token": self.token, "type": "Actor"})
        if res.status_code == 200:
            self.uuid = res.text
            return True
        else:
            return False

    def Logout(self) -> bool:
        res = rq.post(self.rootPath+"Logout", params={"token": self.token})
        if res.status_code == 200:
            return True
        else:
            return False

    def SetInfo(self, info: module.ActorInfo) -> bool:
        res = rq.put(self.rootPath+"Actor/Info", params={"uuid": self.uuid}, data=json.dumps(
            info, cls=module.DefaultEncoder), headers=self.header)
        return res.status_code == 200 and res.json()["success"]

    def GetInfo(self) -> module.ActorInfo:
        res = rq.get(self.rootPath+"Actor/Info", params={"uuid": self.uuid})
        if res.status_code == 204:
            return None
        if res.status_code == 200:
            info: module.ActorInfo = json.loads(
                res.text, object_hook=module.DefaultDecoder)
            return info
        return None

    def SetState(self, state: module.ActorState) -> bool:
        res = rq.put(self.rootPath+"Actor/State", params={"uuid": self.uuid}, data=json.dumps(
            state, cls=module.DefaultEncoder), headers=self.header)
        return res.status_code == 200 and res.json()["success"]

    def GetState(self) -> module.ActorState:
        res = rq.get(self.rootPath+"Actor/State", params={"uuid": self.uuid})
        if res.status_code == 204:
            return None
        if res.status_code == 200:
            info: module.ActorState = json.loads(
                res.text, object_hook=module.DefaultDecoder)
            return info
        return None

    def SetCommand(self, command: module.Command) -> bool:
        jsonStr = json.dumps(command, cls=module.DefaultEncoder)
        res = rq.post(self.rootPath+"Actor/Command",
                      params={"uuid": self.uuid}, data=jsonStr, headers=self.header)
        return res.status_code == 200 and res.json()["success"]

    def GetCommand(self) -> module.Command:  
        res = rq.get(self.rootPath+"Actor/Command", params={"uuid": self.uuid})
        if res.status_code == 204:
            return None
        if res.status_code == 200:
            info: module.Command = json.loads(
                res.text, object_hook=module.DefaultDecoder)
            return info
        return None

    def SetPath(self, path: module.Path) -> bool:
        jsonStr = json.dumps(
            path, cls=module.DefaultEncoder)
        res = rq.put(self.rootPath+"Actor/Path",
                     params={"uuid": self.uuid}, data=jsonStr, headers=self.header)
        return res.status_code == 200 and res.json()["success"]

    '''
    def GetPath(self)->Path:
        res = rq.get(self.rootPath+"Actor/Path", params={"uuid": self.uuid})
        if res.status_code == 204:
            return None
        if res.status_code == 200:
            info:module.Path = json.loads(res.text,object_hook=module.DefaultDecoder)
            return info
        return None
    '''

    '''
    def SendActorEvent(self,event:str):
        #header possible error
        res = rq.put(self.rootPath+"Actor/Event", params={"uuid": self.uuid}, data=event)
        return res.status_code == 200 and res.json()["success"]
    
    def DeleteActorEvent(self):
        res = rq.delete(self.rootPath+"Actor/Event", params={"uuid": self.uuid})
        return res.status_code == 200 and res.json()["success"]
    
    def GetActorEvent(self)->str:
        res = rq.get(self.rootPath+"Actor/Event", params={"uuid": self.uuid})
        if res.status_code == 204:
            return None
        if res.status_code == 200:
            return res.text
    '''

    '''
    def GetAllActorInfos(self)->list:
        res = rq.get(self.rootPath+"Actor/Infos", params={"uuid": self.uuid})
        if res.status_code == 204:
            return None
        if res.status_code == 200:
            info:list = json.loads(res.text,object_hook=module.DefaultDecoder)
            return info
        return None
    
    def SetViewerWatchSet(self,watchSet:list):
        res = rq.put(self.rootPath+"Viewer/WatchSet", params={"uuid": self.uuid}, data=json.dumps(
            watchSet, cls=module.DefaultEncoder), headers=self.header)
        return res.status_code == 200 and res.json()["success"]
    
    def GetActorStatesFromViewerWatchSet(self)->list:
        res = rq.get(self.rootPath+"Viewer/CurrentState", params={"uuid": self.uuid})
        if res.status_code == 204:
            return None
        if res.status_code == 200:
            info:list = json.loads(res.text,object_hook=module.DefaultDecoder)
            return info
        return None
    '''
